package com.xjh.mange.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjh.mange.common.constant.UserSessionName;
import com.xjh.mange.common.enums.DeleteStatusEnum;
import com.xjh.mange.common.util.SessionUtil;
import com.xjh.mange.model.Password;
import com.xjh.mange.model.User;
import com.xjh.mange.service.LogService;
import com.xjh.mange.service.PasswordService;
import com.xjh.mange.vo.LayuiTableVo;
import com.xjh.mange.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 徐佳辉
 * @date 2020/12/23 16:31
 */
@Controller
@RequestMapping("/password")
@Slf4j
public class PasswordController {
    @Autowired
    private PasswordService passwordService;
    @Autowired
    private LogService logService;

    /**
     * 添加或更新账户密码
     *
     * @param password json对象
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    public ResponseVO insert(@RequestBody Password password, HttpSession session) {
        log.info("password:[{}]", password);
        User u = (User) session.getAttribute(UserSessionName.USER);
        if (u == null) {
            return ResponseVO.error("请登录");
        }

        if (password.getId() == null) {
            password.setUserId(u.getUserId());
            password.setDel(DeleteStatusEnum.NOT_DELETE.getCode());
            password.setCreateTime(new Date());
            boolean save = passwordService.save(password);
            logService.Log(u.getUserName(), "添加密码账户:" + password.getCountName());
            return save ? ResponseVO.ok("添加成功") : ResponseVO.error("添加失败");
        } else {
            Password byId = passwordService.getById(password.getId());
            byId.setPassword(password.getPassword());
            byId.setCountName(password.getCountName());
            byId.setUpdateTime(new Date());
            byId.setName(password.getName());
            byId.setState(password.getState());
            boolean b = passwordService.updateById(byId);
            logService.Log(u.getUserName(), "更新密码账户:" + byId.getCountName());
            return b ? ResponseVO.ok("更新成功") : ResponseVO.error("更新失败");

        }

    }

    /**
     * 分页
     *
     * @param page  页码
     * @param limit 每页个数
     * @param name  名称查询条件
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public LayuiTableVo list(int page, int limit, String name) throws Exception {
        if (page < 1) {
            page = 1;
        }
        if (limit < 1) {
            limit = 5;
        }
        IPage<Password> page1 = passwordService.listOrName(page, limit, name);
        List<Password> records = page1.getRecords();
        return LayuiTableVo.ok(records, (int) page1.getTotal());
    }

    /**
     * 根据主键查询，展示的都是
     * 未删除的所以不判断删除状态
     *
     * @param id 主键
     * @return
     */
    @GetMapping("/getOne")
    @ResponseBody
    public ResponseVO getOne(Integer id) {
        Password byId = passwordService.getById(id);
        return ResponseVO.ok(byId);
    }

    /**
     * 删除，更新为删除状态
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("/deleteOne")
    @ResponseBody
    public ResponseVO deleteOne(Integer id, HttpSession session) {
        User u = (User) session.getAttribute(UserSessionName.USER);
        if (u == null) {
            return ResponseVO.error("请登录");
        }
        Password byId = passwordService.getById(id);
        byId.setDel(DeleteStatusEnum.DELETE.getCode());
        boolean b = passwordService.updateById(byId);
        logService.Log(u.getUserName(), "删除密码:" + byId.getCountName());
        return b ? ResponseVO.ok("删除成功") : ResponseVO.error("删除失败");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deletes")
    @ResponseBody
    public ResponseVO delete(String ids, HttpSession session) {
        logService.Log(SessionUtil.getName(session), "批量删除密码:" + ids);
        log.info("id字符串,[{}]", ids);
        String[] s = ids.split(",");
        List<String> list = Arrays.asList(s);
        boolean b = passwordService.removeByIds(list);
        return b ? ResponseVO.ok("删除成功") : ResponseVO.error("删除失败");

    }

}
